/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.web.controller;

import com.bifrost.exception.BonusPointsNotEnoughException;
import com.bifrost.exception.ExternalOrderProcessException;
import com.bifrost.exception.OriginOrderNotFundException;
import com.bifrost.model.ExternalOrder;
import com.bifrost.common.model.ResultModel;
import com.bifrost.model.ReProcessModel;
import com.bifrost.service.BonusPointsService;
import com.bifrost.service.ExternalOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * 积分相关接口
 *
 * @author joewee
 * @version 1.0.0
 * @Date 2018/4/22 21:52
 */
@RestController
@RequestMapping("/api/bonusPoint")
public class BonusPointsController {
    @Autowired
    BonusPointsService bonusPointsService;
    @Autowired
    ExternalOrderService externalOrderService;

    @GetMapping("/query")
    public ResultModel query(@RequestParam String orderNumber, @RequestParam String storeCode, String operator) {
        if (StringUtils.isBlank(orderNumber)) {
            return ResultModel.error("小票号不能为空");
        }
        if (StringUtils.isBlank(storeCode)) {
            return ResultModel.error("门店号不能为空");
        }
        try{
            Integer result = bonusPointsService.check(orderNumber, storeCode);
            //积分足够
            if (result >= 0)
            {
                return ResultModel.ok();
            }
            //积分不足
            else {
                result = Math.abs(result);
                return ResultModel.error(result.toString());
            }
        } catch (OriginOrderNotFundException e) {
            //未找到原单则直接返回ok
            return ResultModel.ok();
        }catch (ExternalOrderProcessException e){
            //系统未注册的门店原单未积分则直接返回ok
            return ResultModel.ok();
        }
    }

    @PostMapping("/order")
    public ResultModel process(@RequestBody @Valid ExternalOrder externalOrder) {
        try {
            Long transactionId = bonusPointsService.processExternalOrder(externalOrder);
            return ResultModel.ok("处理成功").put("transactionId", transactionId);
        } catch (ExternalOrderProcessException e) {
            return ResultModel.error(e.getErrorMsg());
        } catch (BonusPointsNotEnoughException e) {
            return ResultModel.error(e.getErrorMsg());
        } catch (OriginOrderNotFundException e) {
            return ResultModel.error(e.getErrorMsg());
        }
    }
    @PostMapping("/reProcess")
    public ResultModel reProcess(@RequestBody ReProcessModel model ) {
        try {
             externalOrderService.reProces(model.getMarket(),model.getBegin(),model.getEnd());
            return ResultModel.ok("处理成功");
        } catch (ExternalOrderProcessException e) {
            return ResultModel.error(e.getErrorMsg());
        }
    }
    @PutMapping("/exceptionOrder/{id}")
    public ResultModel processExceptionOrder(@PathVariable Long id){
        try {
            Long transactionId = bonusPointsService.processExceptionOrder(id);
            if(transactionId==null){
                return ResultModel.error("处理失败");
            }
            return ResultModel.ok("处理成功").put("transactionId", transactionId);
        } catch (ExternalOrderProcessException e) {
            return ResultModel.error(e.getErrorMsg());
        } catch (BonusPointsNotEnoughException e) {
            return ResultModel.error(e.getErrorMsg());
        } catch (OriginOrderNotFundException e) {
            return ResultModel.error(e.getErrorMsg());
        }
    }
}
